import Vue from 'vue';
import App from './App';
import 'amfe-flexible';
import { router } from './router';
import axios from 'axios';
import { Toast } from 'vant';
import { Dialog } from 'vant';
import Vant from 'vant';
import 'vant/lib/index.css';

Vue.use(Vant);
Vue.use(Toast);
Vue.use(Dialog);

// 配置
const baseUrl = 'http://8.153.73.222:25004';
Vue.prototype.baseUrl = baseUrl;

Vue.prototype.sendReq = (_url, method, _data, cb, errCb) => {
  cb = cb || function () {};
  axios({
    url: baseUrl + _url,
    method: method,
    headers: { 'x-api-token': localStorage.getItem('token') },
    params: method == 'get' ? _data : {},
    data: method == 'post' ? _data : {},
  }).then(r => {
    if (r.data.code == 1) {
      cb(r.data.data)
    } else {
      if (errCb) {
        errCb(r.data)
      } else {
        Toast(r.data.msg)
      }
    }
  }).catch(r => {
    if (r.response.status == 401) {
      router.push('/login')
    }
  })
};

Vue.prototype.formatDate = (_date) => {
  return !_date ? '' : `${_date.getFullYear()}-${('0' + (_date.getMonth() + 1)).slice(-2)}-${('0' + _date.getDate()).slice(-2)}`
};

Vue.prototype.alert = (_msg, cb) => {
  Toast(_msg)
  if (cb) {
    setTimeout(() => { cb(); }, 1000)
  }
};

new Vue({
  router,
  el: '#app',
  render: h => h(App)
});
